import {
  EcsParams,
  EcsSpecificationInfo,
  InstanceListItem,
  InstanceListQueryParams,
  InstanceParams,
  InstanceSaveDto,
} from './model/instanceModel';
import { authorityHttp, deployHttp, tenantHttp } from '/@/utils/http/axios';
import { SaaSMenuListItem } from '/@/api/authority-mgmt/model/roleModel';
import { AppTemplateResp } from '/@/api/operation-center/common/model/commonModel';

enum Api {
  INSTANCE_DISPLAY_LIST = '/v1/instance/instancePage', // TODO: 接口待对齐
  INSTANCE_ADD_OR_UPDATE = '/v1/instance/createOrUpdate',
  INSTANCE_DELETE = '/v1/instance',
  GET_APPS = '/app/listAllApp',
  INSTANCE_TYPE_LIST = '/v1/instance/forApp',
  INSTANCE_VOLUME_LIST = '/v1/instance/orderVolumes',
  INSTANCE_DURATION_LIST = '/v1/instance/durations',
  INSTANCE_ECS_PECIFICATION_INFO = '/v1/instance/getEcsSpecificationInfo',
  INSTANCE_ECS_PECIFICATION_UPDATE = '/v1/instance/updateEcsSpecificationInfo',
  INSTANCE_PRODUCT_BY_SKUCODE = '/v1/instance/getProductId',
  MENU_AND_RESOURCE_TREE = '/appResource/getMenuAndElementResourceTree',
  PACKAGE_DETAIL = '/v1/instance/getAppPackageDetail',
  CHANGE_STATUS = '/v1/instance/changeStatus',
}

export const getInstancePageList = (params: InstanceListQueryParams) => {
  // 获取已选APP
  return deployHttp.get<InstanceListItem[]>({
    url: Api.INSTANCE_DISPLAY_LIST,
    params,
  });
};

export const getEcsSpecificationInfo = (params: EcsParams) => {
  return deployHttp.get<EcsSpecificationInfo>({ url: Api.INSTANCE_ECS_PECIFICATION_INFO, params });
};

export const updateEcsSpecificationInfo = (params: EcsSpecificationInfo) => {
  return deployHttp.post<string>({ url: Api.INSTANCE_ECS_PECIFICATION_UPDATE, params });
};

export const addOrUpdateInstance = (params: InstanceSaveDto) => {
  params.itemIds?.forEach((item) => {
    params.productIds.push(item.productId);
  });
  // 新增或编辑套餐
  return deployHttp.post<string>({ url: Api.INSTANCE_ADD_OR_UPDATE, params });
};

export const deleteInstance = (instanceId: string) => {
  // 删除套餐
  return deployHttp.delete<string>({
    url: Api.INSTANCE_DELETE + '/' + instanceId,
  });
};

export const getInstanceTypes = (params: InstanceParams) => {
  // 获取已选APP
  return deployHttp.get<InstanceListItem[]>({
    url: Api.INSTANCE_TYPE_LIST,
    params,
  });
};

export const getProductIdBySkuCode = async (params: any) => {
  const items = await deployHttp.get({ url: Api.INSTANCE_PRODUCT_BY_SKUCODE, params });
  const result = [];
  items.forEach((item) => {
    result.push(item.productId);
  });
  return result;
};

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export const getInstanceDurations = async (appId: string) => {
  // 获取已选APP
  const res = await deployHttp.get<number[]>({
    url: Api.INSTANCE_DURATION_LIST,
  });
  const realResult: any[] = [];
  res.forEach((item) => {
    realResult.push({
      label: item,
      value: item,
    });
  });
  // 转换后的时长选项为
  return realResult;
};

const changeKey = (tree: any) => {
  tree.key = tree.id;
  tree.tempTitle = tree.title;
  tree.title = tree.resourceName;
  if (tree.children !== undefined) {
    tree.children?.forEach((item) => {
      changeKey(item);
    });
  }
};

export const getMenuAndElementResourceTree = async (params: { appId: string }) => {
  const res = await authorityHttp.get<SaaSMenuListItem[]>({ url: Api.MENU_AND_RESOURCE_TREE, params });
  res.forEach((item) => {
    changeKey(item);
  });
  return res;
};

export const getMenuAndElementResourceTreeForResource = async (params: { appId: string }) => {
  const res = await authorityHttp.get<SaaSMenuListItem[]>({ url: Api.MENU_AND_RESOURCE_TREE, params });
  return res;
};

export const getPackageDetail = (params: { id: string }) => {
  return deployHttp.get<InstanceListItem & { resourceIdList: string[] }>({
    url: Api.PACKAGE_DETAIL,
    params,
  });
};

export const changePackageStatus = (params: { status: boolean; id: string }) => {
  return deployHttp.post<string>({ url: Api.CHANGE_STATUS, params });
};

export const getAppsList = () => {
  return tenantHttp.get<AppTemplateResp[]>({ url: Api.GET_APPS });
};
